package ph.gov.marikina.mcsmtms.rest.webservices;

import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import ph.gov.marikina.mcsmtms.domain.MobileUserReport;
import ph.gov.marikina.mcsmtms.services.MobileUserReportService;
import ph.gov.marikina.mcsmtms.services.SettingsService;
import ph.gov.marikina.mcsmtms.utils.Utils;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

@Controller
public class MobileUserReportController {

	@Autowired
	private View jsonView_i;

	@Autowired
	private MobileUserReportService mobileUserReportService;
	
	@Autowired
	private SettingsService settingsService;
	
	private static final Logger log = Logger.getLogger(MobileUserReportController.class.getName());

	@RequestMapping(value = "/mobileUserReports/", method = RequestMethod.GET)
	public ModelAndView getAllMobileUserReports() {

		//List<MobileUserReport> mobileUserReportList = mobileUserReportService.getAllMobileUserReports();
		Date systemDate = settingsService.getDefaultSystemDate();
		List<MobileUserReport> mobileUserReportList = mobileUserReportService.getMobileUserReportsForDate(systemDate);

		ModelAndView mav = new ModelAndView(jsonView_i,
				"mobileUserReportList",
				mobileUserReportList);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/mobileUserReports/create/")
	@ResponseStatus( HttpStatus.CREATED  )
	public ModelAndView createMobileUserReport(@RequestBody String requestBody) {
		Gson gson = new Gson();

		byte[] decoded = Base64.decodeBase64(requestBody.split("=")[1]);
		
		String receivedStr = new String(decoded);
		if((receivedStr.lastIndexOf( '}' ) + 1) <= receivedStr.length()) {
			receivedStr = receivedStr.substring( 0, receivedStr.lastIndexOf( '}' ) + 1);
		}
		
		log.setLevel(Level.INFO);
		log.info("*** DECODED MOBILE USER REPORT DATA: " + receivedStr);
		
        JsonElement jElement = new JsonParser().parse(receivedStr);
        MobileUserReport mobileUserReport = gson.fromJson(jElement, new TypeToken<MobileUserReport>(){}.getType());
        
        log.info("reportDetails: " + mobileUserReport.getDetails());
        // create mobileUserReport
        Date systemDate = settingsService.getDefaultSystemDate();
        mobileUserReport.setReportDateTime(Utils.convertToDefaultSystemDate(mobileUserReport.getReportDateTimeValue(), systemDate));
        mobileUserReportService.addMobileUserReport(mobileUserReport);

		ModelAndView mav = new ModelAndView(jsonView_i,
												"mobileUserReport",
												mobileUserReport);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/mobileUserReports/test1/")
	public ModelAndView testCreate(@RequestBody String mobileUserReport) {
//		List<MobileUserReport> mobileUserReportList = mobileUserReportService.getAllMobileUserReports();

//		ModelAndView mav = new ModelAndView(jsonView_i,
//				"mobileUserReportList",
//				mobileUserReportList);
//		return mav;
		Gson gson = new Gson();
		//try {
		byte[] decoded = Base64.decodeBase64(mobileUserReport.split("=")[1]);
		//(authorization.substring(6).getBytes("UTF-8"));
		String receivedStr = new String(decoded);
		
		//mobileUserReport = Base64.decodeBase64(mobileUserReport.split("=")[1]).toString();
			//(str.getBytes("UTF-8")).toString();
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//			throw new UnsupportedOperationException(e);
//		}
		
        JsonElement jElement = new JsonParser().parse(receivedStr);
        //JsonObject jObject = jElement.getAsJsonObject();
        MobileUserReport mobileUserReport2 = gson.fromJson(jElement, new TypeToken<MobileUserReport>(){}.getType());
		
        ModelAndView mav = new ModelAndView(jsonView_i, "mobileUserReport", mobileUserReport2);
        
        return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/mobileUserReports/test2/")
	public ModelAndView testCreate(@RequestBody MobileUserReport str) {
		List<MobileUserReport> mobileUserReportList = mobileUserReportService.getAllMobileUserReports();

		ModelAndView mav = new ModelAndView(jsonView_i,
				"mobileUserReportList",
				mobileUserReportList);
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/mobileUserReports/test3/")
	public ModelAndView testPost3(@RequestBody String mobileUserReport) {
        ModelAndView mav = new ModelAndView(jsonView_i, "mobileUserReport", mobileUserReport);
        
        return mav;
	}
	
//	@RequestMapping(value = "/userreports/{id}", method = RequestMethod.GET)
//	public ModelAndView getAllSituationReports(@PathVariable( "id" ) String id) {
//
//		List<MobileUserReport> situationReportList = mobileUserReportService.getAllMobileUserReports();
//
//		ModelAndView mav = new ModelAndView(jsonView_i,
//				"mobileUserReportList",
//				situationReportList);
//		return mav;
//	}
	
	
}
